package com.jd.fruit.common.config;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.alibaba.druid.wall.WallFilter;
import com.jd.fruit.common.handler.GlobalHandler;
import com.jd.fruit.common.plugin.ControllerPlugin;
import com.jd.fruit.common.plugin.TablePlugin;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

public class DriveConfig extends JFinalConfig {

	private static Logger log = Logger.getLogger(DriveConfig.class);
	
	@Override
	public void configConstant(Constants me) {
		me.setDevMode(true);
		this.loadPropertyFile("jfinal.properties");
		me.setMainRenderFactory(new BeetlRenderFactory());
		me.setError404View("/common/error404.html");
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new GlobalHandler());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		log.info("Load configInterceptor.....");
	}

	@Override
	public void configPlugin(Plugins me) {
		log.info("Load Plugin BEGIN.....");
		String jdbcUrl = getProperty("jdbcUrl");
		String driver = getProperty("driverClass");
		String username = getProperty("username");
		String password = getProperty("password");
		
		// Druid Datasource
		DruidPlugin druidPlugin = new DruidPlugin(jdbcUrl, username, password, driver);
		WallFilter wallFilter = new WallFilter();
		wallFilter.setDbType("mysql");
		druidPlugin.addFilter(wallFilter);
		me.add(druidPlugin);
		
		
		//数据映射
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		me.add(arp);
		Map<String, ActiveRecordPlugin> arpMap = new HashMap<String, ActiveRecordPlugin>();
		arpMap.put("deafult", arp);
		arp.setShowSql(true);//这句话就是ShowSql
		new TablePlugin(arpMap).start();
		me.add(arp); // 数据表映射
		
		
		log.info("Load Plugin END.....");
		
	}

	@Override
	public void configRoute(Routes me) {
		log.info("Load Routes BEGIN.....");
		new ControllerPlugin(me).start();
		log.info("Load Routes END.....");
	}
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}

}
